package com.wn.interceptor;

import com.wn.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 *   拦截器的开发步骤:
 *     1、实现 HandlerInterceptor 接口
 *
 *     2、重写preHandle() 方法  拦截controller请求
 *            判断session 是否有user
 *     3、配置拦截器
 *            拦截谁
 *
 */
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        //获取查看session
//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("user");
//        if (Objects.nonNull(user)){
//            return true;  //放行
//        }
//        //跳转到登录页面http://localhost:8080/login.html
//        response.sendRedirect("http://localhost:8080/login.html");
//        return false;  //不能访问到 controller
        return true;
    }
}